package com.TeamJigsaw.DanteTrail.Views;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.TeamJigsaw.DanteTrail.Models.GameSprite;
import com.TeamJigsaw.DanteTrail.Models.Party;

public class HuntingGameDisplay extends JPanel{

	private Party myParty;
	private GameSprite player;
	private GameSprite bullet;
	private Point bulletTarget;
	private boolean bulletReachedDest;
	private boolean bulletFlying;
	private List<GameSprite> monsters;
	private Image bg;
	private int food;
	private int myAmmo;
	private Random generator;
	public final int WIDTH = 500;
	public final int HEIGHT = 300;
	private final int MONSTER_NO = 3;
	int dy;
	int dx;
	
	public HuntingGameDisplay(Party myParty, int level) {
		this.myParty = myParty;
		Image bulletImage = new ImageIcon("resources\\bullet.gif").getImage();
		generator = new Random();
		monsters = new ArrayList<GameSprite>();
		player = (new GameSprite(WIDTH/2, HEIGHT/2, myParty.getPlayer().getPicture()));
		bullet = (new GameSprite(WIDTH/2, HEIGHT/2, bulletImage));
		bulletTarget = new Point(0,0);
		bulletReachedDest = false;
		bulletFlying = false;
		food = 0;
		myAmmo = myParty.getAmmo();
		setPreferredSize(new Dimension(500,300));
		addMouseListener(new myMouseListener());
		repaint();
		dy = 0;
		dx = 0;
		bg = new ImageIcon("resources\\" + level + "1bgm.gif").getImage();

	}
	
	private void update() {
		
		//randomly generate monsters
		if (generator.nextInt() % 10 == 0 && monsters.size() < 10)
		{
			GameSprite newMonster = new GameSprite(48/2+1, generator.nextInt(HEIGHT), 
					new ImageIcon("resources\\monster" + 
					(generator.nextInt(MONSTER_NO)+1) + ".gif").getImage(),48,48);
			newMonster.xSpeed = generator.nextInt(5);
			newMonster.ySpeed = generator.nextInt(5)-3;
			monsters.add(newMonster);
		}
		
		//update monster motion
		for (int i = 0; i < monsters.size(); i++) {
			GameSprite curSprite = monsters.get(i);
			curSprite.update();
			
			if (getDistance(curSprite.position, bullet.position) < 64)
				if (bulletFlying) {
					monsters.remove(curSprite);
					food+=5;
				}
		
			if (curSprite.position.y <= 0 ||
					curSprite.position.y >= HEIGHT || 
					curSprite.position.x <= 0 || 
					curSprite.position.x >= WIDTH ) {
				monsters.remove(curSprite);

			} 
		}
		
		//update bullet motion
		if (bulletFlying) {
			
			if (Math.abs(getDistance(bullet.position,bulletTarget)) >= 10 && bulletReachedDest == false)
			{
				dy = bulletTarget.y - bullet.position.y;
				dx = bulletTarget.x - bullet.position.x;
				bulletReachedDest = true;
			}
			
			double a = Math.atan2(dy, dx);
			bullet.xSpeed = (10*Math.cos(a));
			System.out.println("" + bullet.xSpeed);
			bullet.ySpeed = (10*Math.sin(a));
			System.out.println("" + bullet.ySpeed);
			
			bullet.update();
			
			if (bullet.position.y == bulletTarget.y && bullet.position.x == bulletTarget.x)
				bulletReachedDest = true;
		
			if (bullet.position.y <= 0 || 
					bullet.position.x <= 0 || 
					bullet.position.x >= WIDTH || 
					bullet.position.y >= HEIGHT) {
				bulletFlying = false;
			}
		}		
	}
	
	private double getDistance(Point a, Point b)
	{
		return Math.sqrt(Math.pow(a.x-b.x, 2) + Math.pow(a.y-b.y,2));
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(bg,0,0,null);
		update();
		player.draw(g);
		if (bulletFlying) {
			g.setColor(Color.BLACK);
			g.fillOval(bullet.position.x-16, bullet.position.y-16, 16, 16);
		}
		for (GameSprite curSprite: monsters) {
			curSprite.draw(g);
		}
		g.setColor(Color.BLACK);
		g.fillRect(0, HEIGHT-32, WIDTH, 32);
		g.setColor(Color.WHITE);
		g.drawString("Ammo: " + myAmmo, 16, HEIGHT-16);
		g.drawString("Food Collected:" + food, 100, HEIGHT-16);
		
		try {
			Thread.sleep(15);
		}
		catch(Exception e){
			System.out.println("Process Interrupted");
		}
		
		repaint();
	}
	
	public void endGame() {
		myParty.setAmmo(myAmmo);
		myParty.addFood(food);
	}
	
	private class myMouseListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {}

		@Override
		public void mouseEntered(MouseEvent e) {}

		@Override
		public void mouseExited(MouseEvent e) {}

		@Override
		public void mousePressed(MouseEvent e) {}

		@Override
		public void mouseReleased(MouseEvent e) {
			if (!bulletFlying && myAmmo > 0) {
				bulletTarget = e.getPoint();
				System.out.println(""+bulletTarget);
				bullet.position.y = player.position.y + player.width / 2;
				bullet.position.x = player.position.x + player.height / 2;
				bulletReachedDest = false;
				bulletFlying = true;
				dy = bullet.position.y - bulletTarget.y;
				dx = bullet.position.x - bulletTarget.x;
				myAmmo--;
			}
		}
	}
}
